package com.ht.pms.core.controller.recruit;

import com.ht.pms.core.common.exception.ServiceException;
import com.ht.pms.core.dto.PageResult;
import com.ht.pms.core.dto.Result;
import com.ht.pms.core.dto.recruit.*;
import com.ht.pms.core.service.RecruitService;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/recruit")
public class RecruitController {


    private static final Logger LOG = LoggerFactory.getLogger(RecruitController.class);


   @Resource
    private RecruitService recruitService;


    @GetMapping(value = "/recruitList")
    @ResponseBody
    @ApiOperation("获取招聘列表")
//    public PageResult recruitList(@RequestBody RecruitQueryDto dto) throws Exception {
    public PageResult recruitList(String typeId, String stationName, String stationPlace, String startTime, String endTime, String state, int page, int limit) throws Exception {
        LOG.info("=================>>>>获取招聘列表接口开始");
        PageResult result = null;
        try {
            result = recruitService.recruitListForQuery(typeId, stationName, stationPlace, startTime, endTime, state, page, limit);

        } catch (ServiceException se) {
            LOG.error("=============>>>>获取招聘列表出错", se);
            result = PageResult.error(se.getErrorCode(), se.getErrorMsg());
        } catch (Exception e) {
            result = PageResult.error(9999, e.getMessage());
            LOG.error("=============>>>>获取招聘列表出错", e);
        }
        LOG.info("=================>>>>获取招聘列表结束{}", result);
        return result;
    }

    @GetMapping(value = "/recruitQuery")
    @ResponseBody
    @ApiOperation("单个招聘信息查询")
    public Result recruitQuery(String id) throws Exception {
        LOG.info("=================>>>>单个招聘信息查询开始");
        Result result = null;
        try {
            RecruitDetailWeb recruitDetail = recruitService.recruitQuery(id);
            result = Result.buildSuccess(recruitDetail);
        } catch (ServiceException se) {
            LOG.error("=============>>>>单个招聘信息查询出错", se);
            result = Result.buildFail(se.getCodeDesc(), se.getErrorMsg());
        } catch (Exception e) {
            result = Result.buildFail();
            LOG.error("=============>>>>单个招聘信息查询出错", e);
        }
        LOG.info("=================>>>>单个招聘信息查询结束{}", result);
        return result;
    }

    @PostMapping(value = "/addOrEditRecruit")
    @ResponseBody
    @ApiOperation("招聘信息新增/修改")
    public Result addOrEditRecruit(@RequestBody @Valid RecruitDetailQuery recruitDetailQuery) throws Exception {
        LOG.info("=================>>>>招聘信息新增/修改开始");
        Result result = null;
        try {
//            recruitService.addOrEditRecruit(opType,id, typeId, stationName, stationPlace, stationSalary, stationNumer, startTime, endTime, state, stationSeniority, stationDuty);
            recruitService.addOrEditEntityRecruit(recruitDetailQuery);
            result = Result.buildSuccess();
        } catch (ServiceException se) {
            LOG.error("=============>>>>招聘信息新增/修改出错", se);
            result = Result.buildFail(se.getCodeDesc(), se.getErrorMsg());
        } catch (Exception e) {
            result = Result.buildFail();
            LOG.error("=============>>>>招聘信息新增/修改出错", e);
        }
        LOG.info("=================>>>>招聘信息新增/修改结束{}", result);
        return result;
    }

    @GetMapping(value = "/getProviceCity")
    @ResponseBody
    @ApiOperation("获取省或市")
    public Result getProviceCity( String  provinceId) throws Exception {
        LOG.info("=================>>>>获取省或市开始");
        Result result = null;
        try {
//            String provinceId = provinceCitysReq.getProvinceId();
            List<ProvinceCitys> nameList = recruitService.getProviceCity(provinceId);
            result = Result.buildSuccess(nameList);
        } catch (ServiceException se) {
            LOG.error("=============>>>>获取省或市出错", se);
            result = Result.buildFail(se.getCodeDesc(), se.getErrorMsg());
        } catch (Exception e) {
            result = Result.buildFail();
            LOG.error("=============>>>>获取省或市出错", e);
        }
        LOG.info("=================>>>>获取省或市结束{}", result);
        return result;
    }
    @PostMapping(value = "/getProviceCityApp")
    @ResponseBody
    @ApiOperation("获取省市微信用")
    public Result getProviceCityApp() throws Exception {
        LOG.info("=================>>>>获取省市微信用开始");
        Result result = null;
        try {
            List<Province> provinceList = recruitService.getProviceCityApp();
            result = Result.buildSuccess(provinceList);
        } catch (ServiceException se) {
            LOG.error("=============>>>>获取省市微信用出错", se);
            result = Result.buildFail(se.getCodeDesc(), se.getErrorMsg());
        } catch (Exception e) {
            result = Result.buildFail();
            LOG.error("=============>>>>获取省市微信用出错", e);
        }
        LOG.info("=================>>>>获取省市微信用结束{}", result);
        return result;
    }

    @PostMapping(value = "/recruitListApp")
    @ResponseBody
    @ApiOperation("app获取招聘列表")
    public Result recruitListApp(@RequestBody RecruitDetailAppReq recruitDetailAppReq) throws Exception {
        LOG.info("=================>>>>app获取招聘列表开始");
        Result result = null;
        try {
            List<RecruitDetailApp> list = recruitService.recruitListApp(recruitDetailAppReq);
            result = Result.buildSuccess(list);
        } catch (ServiceException se) {
            LOG.error("=============>>>>app获取招聘列表出错", se);
            result = Result.buildFail(se.getCodeDesc(), se.getErrorMsg());
        } catch (Exception e) {
            result = Result.buildFail();
            LOG.error("=============>>>>app获取招聘列表出错", e);
        }
        LOG.info("=================>>>>app获取招聘列表结束{}", result);
        return result;
    }


}
